package com.shankungfu.adapter;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.shankungfu.R;
import com.shankungfu.bean.FindDataBean;
import com.shankungfu.util.PhoneUtils;

public class BrowseAdapter extends BaseAdapter {

	private Context context;
	private List<FindDataBean> findData;
	private DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	private ImageLoader imageLoader;

	public BrowseAdapter(Context context, DisplayImageOptions options) {
		this.context = context;
		this.options = options;
		imageLoader = ImageLoader.getInstance();
	}

	public void setData(List<FindDataBean> findData) {
		this.findData = findData;
	}
	public void addData(List<FindDataBean> findData) {
		this.findData.addAll(findData);
	}

	@Override
	public int getCount() {
		return findData != null ? findData.size() : 0;
	}

	@Override
	public FindDataBean getItem(int position) {
		return findData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.browse_item_layout,
					null);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.date = (TextView) convertView.findViewById(R.id.date);
			holder.address = (TextView) convertView.findViewById(R.id.address);
			holder.need_time = (TextView) convertView
					.findViewById(R.id.need_time);
			holder.favorite_pepole = (TextView) convertView
					.findViewById(R.id.browse_favorite_pepole);
			holder.join_pepole = (TextView) convertView
					.findViewById(R.id.browse_join_pepole);
			holder.item_kind = (TextView) convertView
					.findViewById(R.id.item_kind);
			holder.item_pic = (ImageView) convertView
					.findViewById(R.id.item_pic);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		FindDataBean dataBean = findData.get(position);

		holder.title.setText(dataBean.getFind_title());

		String startTime = PhoneUtils.TimeStamp2DateForSKF(dataBean
				.getFind_time().getStime());
		String endTime = PhoneUtils.TimeStamp2DateForSKF(dataBean
				.getFind_time().getEtime());

		holder.date.setText("时间：" + startTime + " ~ " + endTime);

		holder.address.setText("地点：" + dataBean.getFind_address());
		holder.need_time.setText("所需志愿时：" + dataBean.getFind_services_time()
				+ "小时");
		holder.favorite_pepole.setText(dataBean.getPeople_number());
		holder.join_pepole.setText(dataBean.getJoin_number());
		holder.item_kind.setText(dataBean.getFind_type().toString());

		imageLoader.displayImage(dataBean.getImg_url(), holder.item_pic,
				options, animateFirstListener);

		return convertView;
	}

	public static boolean isNum(String ss) {
		for (int i = 0; i < ss.length(); i++) {
			char a = ss.charAt(i);
			if (a < '0' || a > '9') {
				return false;
			}
		}
		return true;
	}

	private class ViewHolder {
		private TextView title;
		private TextView date;
		private TextView address;
		private TextView need_time;
		private TextView favorite_pepole;
		private TextView join_pepole;
		private TextView item_kind;
		private ImageView item_pic;
	}

	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}

}
